#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <windows.h>

struct TestOption {
    int id;
    std::string name;
    std::string executable;
};

void printMenu(const std::vector<TestOption>& tests) {
    system("cls");
    std::cout << "==========================================" << std::endl;
    std::cout << "           SimpleDB Test Runner           " << std::endl;
    std::cout << "==========================================" << std::endl;
    std::cout << "Please select a test to run by entering the corresponding number:" << std::endl;
    
    for (const auto& test : tests) {
        std::cout << "[" << test.id << "] " << test.name << std::endl;
    }
    
    std::cout << "[0] Exit" << std::endl;
    std::cout << "==========================================" << std::endl;
}

// Get the path of the current executable
std::string getExecutablePath() {
    char buffer[MAX_PATH];
    GetModuleFileName(NULL, buffer, MAX_PATH);
    std::string::size_type pos = std::string(buffer).find_last_of("\\/");
    return std::string(buffer).substr(0, pos);
}

int main() {
    // Define all available tests
    std::vector<TestOption> tests = {
        {1, "Lexer Test", "test_lexer.exe"},
        {2, "Parser Test", "test_parser.exe"},
                {3, "Planner Test", "test_planner.exe"},
        {4, "Semantic Analyzer Test", "test_semantic_analyze.exe"},
        {5, "Catalog Test", "test_catalog_new.exe"},

        {6, "Executor Test", "test_executor_ultimate.exe"},
        {7, "Expression Evaluator Test", "test_expression_evaluator.exe"},
        {8, "Buffer Pool Test", "test_buffer_pool.exe"},
        {9, "Disk Manager Test", "test_disk_manager.exe"},
        {10, "Page Test", "test_page.exe"},



    };
    
    // Get the directory of the current executable
    std::string exePath = getExecutablePath();
    std::string exeDir;
    
    // Find the project root directory (db_system)
    size_t projectRootPos = exePath.find("\\db_system\\");

        // Construct the path to build\Debug directory
        std::string projectRoot = exePath.substr(0, projectRootPos + 11); // +9 for "\db_system\" length
        exeDir = projectRoot + "\\build\\Debug";

    
    int choice;
    
    while (true) {
        printMenu(tests);
        std::cout << "Enter your choice: ";
        
        // Read user input
        std::cin >> choice;
        
        // Check for exit
        if (choice == 0) {
            std::cout << "Exiting Test Runner. Goodbye!" << std::endl;
            break;
        }
        
        // Find and execute the selected test
        bool found = false;
        for (const auto& test : tests) {
            if (test.id == choice) {
                found = true;
                std::cout << "Running " << test.name << "..." << std::endl;
                
                // Build the full command with path
                std::string command = exeDir + "\\" + test.executable;
                
                // For Windows, run the command directly
                int result = system(command.c_str());
                
                std::cout << "\nTest completed with exit code: " << result << std::endl;
                std::cout << "Press Enter to continue...";
                
                // Clear input buffer
                std::cin.ignore();
                std::cin.get();
                break;
            }
        }
        
        if (!found) {
            std::cout << "Invalid choice. Please try again." << std::endl;
            std::cout << "Press Enter to continue...";
            
            // Clear input buffer
            std::cin.ignore();
            std::cin.get();
        }
    }
    
    return 0;
}